package com.oms.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.liudecai.utils.jdbc.TxQueryRunner;
import com.oms.bean.ShoppingCar;

public class ShoppingCarDao {
	QueryRunner qr=null;
	public ShoppingCarDao() {
		qr = new TxQueryRunner();
	}
	public void Add(ShoppingCar shoppingcar) throws SQLException {
		String sql = "insert into oms_shopping_cart(shopping_cart_id,user_id,food_price,food_number,add_time) values(?,?,?,?,?)";
		qr.update(sql, shoppingcar.getShopping_cart_id(),shoppingcar.getUser_id(),shoppingcar.getFood_price(),shoppingcar.getFood_number(),shoppingcar.getAdd_time());
	}

	public void Delete(int id) throws SQLException {
		String sql = "delete from oms_shopping_cart where id=?";
		qr.update(sql, id);
	}

	public void Update(ShoppingCar shoppingcar) throws SQLException {
		String sql = "update oms_shopping_cart set shopping_cart_id=?,user_id=?,food_price=?,food_number=?,add_time=? where id=?";
		qr.update(sql, shoppingcar.getShopping_cart_id(),shoppingcar.getUser_id(),shoppingcar.getFood_price(),shoppingcar.getFood_number(),shoppingcar.getAdd_time(),shoppingcar.getId());
	}

	public ShoppingCar FindById(int id) throws SQLException {
		String sql = "select * from oms_shopping_cart where id=?";
		return qr.query(sql, new BeanHandler<>(ShoppingCar.class),id);

	}

	public List<ShoppingCar> FindByString(String str) throws SQLException {
		String sql = "select * from oms_shopping_cart";
		return qr.query(sql, new BeanListHandler<>(ShoppingCar.class));
	}

	public List<ShoppingCar> FindAll() throws SQLException {
		String sql = "select * from oms_shopping_cart";
		return qr.query(sql, new BeanListHandler<>(ShoppingCar.class));
	}

	public long ShoppingCarCount() throws SQLException {
		String sql = "select count(id) from oms_shopping_cart";
		return qr.query(sql, new ScalarHandler<>());
	}

}
